package com.megvii.koala.tv.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.megvii.corelib.components.BaseNetworkErrorView;
import com.megvii.koala.base.KoalaPresenter;
import com.megvii.koala.tv.R;
import com.megvii.koala.tv.event.TVEventAPI;
import com.megvii.koala.tv.fragment.param.MenuParam;
import com.megvii.koala.tv.presenter.MenuPresenter;

import butterknife.Bind;
import butterknife.OnClick;
import butterknife.OnFocusChange;

/**
 * Created by linyue on 16/1/23.
 */
public class MenuFragment extends BaseFragment<MenuParam>
{
    protected View rootView;

    @Bind(R.id.menu_info)
    protected TextView infoText;

    @Bind(R.id.menu_register)
    protected View registerView;

    @Bind(R.id.menu_refresh)
    protected View refreshView;

    private MenuListener listener;

    private boolean firstCreatView = true;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState)
    {
        if (rootView == null)
        {
            rootView = inflater.inflate(R.layout.menu_layout, container, false);
        }
        return rootView;
    }

    public void setRegisterButtonEnable(boolean enable)
    {
        registerView.setEnabled(enable);
        registerView.setFocusable(enable);
        registerView.setClickable(enable);
        if (!enable)
        {
            refreshView.requestFocus();
        }
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState)
    {
        super.onViewCreated(view, savedInstanceState);
        if (firstCreatView)
        {
            firstCreatView = false;
            registerView.requestFocus();
        }
    }

    @OnFocusChange(R.id.menu_register)
    public void onRegisterFocused(View v, boolean hasFocus)
    {
        if (hasFocus)
        {
            infoText.setText("当要注册的人脸出现在画面中时，点击“一键注册”");
            TVEventAPI.sendMenuFocusEvent(true);
        }
        else
        {
            TVEventAPI.sendMenuFocusEvent(false);
        }
    }

    @OnFocusChange(R.id.menu_refresh)
    public void onRefreshFocused(View v, boolean hasFocus)
    {
        if (hasFocus)
        {
            infoText.setText("重新加载迎宾界面");
        }
    }

    @OnFocusChange(R.id.menu_theme)
    public void onThemeFocused(View v, boolean hasFocus)
    {
        if (hasFocus)
        {
            infoText.setText("更换当前迎宾界面主题");
        }
    }

    @OnFocusChange(R.id.menu_settings)
    public void onSettingsFocused(View v, boolean hasFocus)
    {
        if (hasFocus)
        {
            infoText.setText("更改系统设置");
        }
    }

    @OnClick(R.id.menu_register)
    public void onClickRegister(View view)
    {
        if (listener != null)
        {
            listener.onClickRegister();
        }
    }

    @OnClick(R.id.menu_refresh)
    public void onClickRefresh(View view)
    {
        if (listener != null)
        {
            listener.onClickRefresh();
        }
    }

    @OnClick(R.id.menu_theme)
    public void onClickTheme(View view)
    {
        if (listener != null)
        {
            listener.onClickTheme();
        }
    }

    @OnClick(R.id.menu_settings)
    public void onClickSettings(View view)
    {
        if (listener != null)
        {
            listener.onClickSettings();
        }
    }

    @Override
    public BaseFragmentListener getBaseFragmentListener()
    {
        return listener;
    }

    @Override
    public Class<? extends KoalaPresenter> getPresenterClass()
    {
        return MenuPresenter.class;
    }

    @Override
    public BaseNetworkErrorView getNetworkErrorView()
    {
        return null;
    }

    public void setListener(MenuListener listener)
    {
        this.listener = listener;
    }

    public interface MenuListener extends BaseFragment.BaseFragmentListener
    {
        public void onClickRegister();

        public void onClickRefresh();

        public void onClickTheme();

        public void onClickSettings();
    }
}
